package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.UserRegisterRequest;
import com.cskaoyan.user.dto.UserRegisterResponse;
import com.cskaoyan.user.dto.UserVerifyRequest;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.service.IUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 叶熠彬
 * @description: 用户注册
 * @date: 2022/8/1 10:38
 */
@RestController
// @RequestMapping("user")
public class UserRegisterController {


    @Autowired
    IUserRegisterService userRegisterService;

    /**
     * 用户注册
     *
     * @author: 叶熠彬
     * @date: 2022/8/1 11:07
     * @param: [request]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */
    // @PostMapping("register")
    public ResponseData registerUser(@RequestBody UserRegisterRequest request) {
        UserRegisterResponse response = userRegisterService.register(request);
        if (response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }

    /**
     * 邮箱激活
     *
     * @author: 叶熠彬
     * @date: 2022/8/1 11:07
     * @param: [request]
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     */
    // @PostMapping("verify")
    public ResponseData verifyUser(@RequestBody UserVerifyRequest request) {
        UserVerifyResponse response = userRegisterService.verify(request);
        if (response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(Integer.parseInt(response.getCode()), response.getMsg());
    }


}
